package org.robot.scm.platform.registry;

import static java.lang.Runtime.getRuntime;

import lombok.extern.slf4j.Slf4j;
import org.robot.scm.platform.AbstractMonitor;
import org.robot.scm.platform.AbstractRegistry;
import org.robot.scm.pojos.config.base.BasePlatform;
import org.robot.scm.pojos.enums.PlatformEnum;

/**
 * 2proxy socks代理
 *
 * @Author : Awn create on 2024/5/12 19:36
 */
@Slf4j
public class Proxy3Registry extends AbstractRegistry {

    private static Process proxy3;

    @Override
    public int getOrder() {
        return 1;
    }

    @Override
    public void registry() {
        if (!AbstractMonitor.mainConfig.getEnables().contains(PlatformEnum.PROXY3.getVal())) {
            return;
        }
        new Thread(Proxy3Registry::runProxy3).start();
        getRuntime().addShutdownHook(new Thread(() -> {
            proxy3.destroyForcibly();
            log.info("{} destroy ... ok", PlatformEnum.PROXY3.getVal());
        }));
        log.info("{} shutdown registry ... ok", PlatformEnum.PROXY3.getVal());
    }

    private static void runProxy3() {
        try {
            BasePlatform platform = AbstractMonitor.mainConfig.getPlatform(PlatformEnum.PROXY3);
            proxy3 = getRuntime().exec(platform.getNoticeSrc() + "3proxy.exe " + platform.getNoticeSrc() + "3proxy.cfg");
            proxy3.waitFor();
        } catch (Exception e) {
            log.error("{} fail, error: {}", PlatformEnum.PROXY3.getVal(), e.getMessage());
        }
    }
}
